/*
 * This file is part of Cleanflight.
 *
 * Cleanflight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cleanflight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cleanflight.  If not, see <http://www.gnu.org/licenses/>.
 */

// This file is copied with modifications from project Deviation,
// see http://deviationtx.com

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include <platform.h>

#include "build/build_config.h"
#include "build/debug.h"

#include "drivers/adc.h"
#include "drivers/exti.h"
#include "drivers/bus_spi.h"
#include "drivers/nvic.h"
#include "drivers/io.h"
#include "drivers/light_led.h"
#include "drivers/io_impl.h"
#include "drivers/rx_spi.h"
#include "drivers/pwm_output.h"
#include "drivers/rx_nrf24l01.h"
#include "drivers/time.h"
#include "drivers/max7219.h"

#include "fc/runtime_config.h"

#include "common/crc.h"

#include "flight/imu.h"
#include "flight/altitude.h"

#ifdef USE_RX_NRF24

//0~127
#define CHANNEL 0x02

#define RX_TX_ADDR_LEN 3
#define ADDR_WIDTH NRF24L01_03_SETUP_AW_3BYTES
// #define ADDR_WIDTH NRF24L01_03_SETUP_AW_4BYTES
// #define ADDR_WIDTH NRF24L01_03_SETUP_AW_5BYTES
uint8_t txAddr[RX_TX_ADDR_LEN] = {0x15, 0x0F, 0x71};
uint8_t rxAddr[RX_TX_ADDR_LEN] = {0x15, 0x0F, 0x71};

//#define DATA_RATE NRF24L01_06_RF_SETUP_RF_DR_2Mbps
#define DATA_RATE NRF24L01_06_RF_SETUP_RF_DR_1Mbps
//#define RF_PWR NRF24L01_06_RF_SETUP_RF_PWR_n18dbm
//#define RF_PWR NRF24L01_06_RF_SETUP_RF_PWR_n12dbm
//#define RF_PWR NRF24L01_06_RF_SETUP_RF_PWR_n6dbm
#define RF_PWR NRF24L01_06_RF_SETUP_RF_PWR_0dbm

#define NRF24_CE_HI()   {IOHi(DEFIO_IO(RX_CE_PIN));}
#define NRF24_CE_LO()   {IOLo(DEFIO_IO(RX_CE_PIN));}

#define R_REGISTER    0x00
#define W_REGISTER    0x20
#define REGISTER_MASK 0x1F
#define ACTIVATE      0x50
#define R_RX_PL_WID   0x60
#define R_RX_PAYLOAD  0x61
#define W_TX_PAYLOAD  0xA0
#define W_ACK_PAYLOAD 0xA8
#define FLUSH_TX      0xE1
#define FLUSH_RX      0xE2
#define REUSE_TX_PL   0xE3
#define NOP           0xFF

#define DATA_SIZE 12

#define PAYLOAD_SIZE (DATA_SIZE+4)

struct ControlData{
    int16_t att_roll;//2
    int16_t att_pitch;//2
    uint8_t throttle;//1
    uint8_t yaw;//1
    uint8_t x;//1
    uint8_t y;//1
    uint8_t manual;//1
    uint8_t aux;//1
};

struct TelemData{
    int16_t alt;
    int16_t volt;
};

struct CrcData{
    uint8_t data[DATA_SIZE];//校验要初始化清零
    uint32_t crc;
};

union Buff{
    uint8_t arr[PAYLOAD_SIZE];
    struct ControlData controlData;
    struct TelemData telemData;
    struct CrcData crcData;
};

//bool protocolDataReceived;


static union Buff buff;

uint16_t rawData[6] = {1000, 1000, 1000, 1000, 1000, 1000};

static IO_t irqIO;
static IO_t btnIO;
static IO_t btn2IO;

       
static const int num10s[] = {
	1000000000,
	100000000,
	10000000,
	1000000,
	100000,
	10000,
	1000,
	100,
	10,
	1,
};
static uint8_t charbuff[8];
static void printInt(int val, int len, int offset){//max len = 9
	int idx;
	int i;
	int num;
	if (val >= 0){
		num = val;
		//putchar('+');
	}
	else{
		num = num10s[9-len] + val;
		//putchar('-');
	}
	int outNum;
    int bidx = 0;
	for (idx = 0; idx < 10; idx++)
	{
		outNum = 0;
		int CmpNum = num10s[idx];
		for (i = 0; i < 10; i++)
		{
			if (num >= CmpNum)
			{
				num -= CmpNum;
				outNum++;
			}
			else
			{
				break;
			}
		}
		if (idx > 9 - len){
			//serialWrite(serialPort, '0' + outNum);
            charbuff[offset + bidx++] = outNum;
		}
	}
}

uint8_t manual;
uint32_t rx_count;
float target_att[2];
static float accu = 0;
uint8_t nrf24l01_x = 51;
uint8_t nrf24l01_y = 51;
static int oldFrameCount;
static int totalCount = 0;
static int errorCount = 0;
static int status = 1;//sending
static int receiveCount = 0;
bool nrf24l01FrameStatus(){
#ifdef I_AM_REMOTE_CONTROLLER
    UNUSED(receiveCount);
    static int flg;
    bool irq = IORead(irqIO);
    if (!irq)//下降
    {
        if(status==1){//发送完成
            uint8_t s = NRF24L01_ReadReg(NRF24L01_07_STATUS);
            if(s & BV(NRF24L01_07_STATUS_TX_DS)){// 已发送
                NRF24L01_ClearAllInterrupts();
                //进入接收模式
                NRF24L01_SetRxMode();
            }
            status=0;
        }else{
            //接收遥测数据
            uint8_t s = NRF24L01_ReadReg(NRF24L01_07_STATUS);
            if( s & BV(NRF24L01_07_STATUS_RX_DR)){// 已接收
                NRF24L01_ReadPayload(buff.arr, PAYLOAD_SIZE);

                uint32_t crc = 0;
                crc = crc32(crc, buff.arr, DATA_SIZE);
                totalCount++;
                if(buff.crcData.crc != crc){
                    errorCount++;
                    charbuff[4] = 0x0E;//error
                    Write_Char_Max7219(4,charbuff[4]);
                }else{
                    //version bit top bit 1: oldframe
                    uint16_t adcSample = buff.telemData.volt;
                    uint16_t oldFrame = adcSample & 0x8000;
                    if (oldFrame) {
                        if (oldFrameCount < 100) {
                            oldFrameCount++;
                        }
                    } else {
                        if (oldFrameCount > -100) {
                            oldFrameCount--;
                        }
                    }
                    adcSample = adcSample & (~0x8000);
                    //debuginfoFastInt[2] = adcSample;
                    float temp;
                    if(adcSample<4000){
                        temp = adcSample;
                        if(oldFrameCount>0){
                            temp = temp - 2358;
                            temp = temp / (3753-2358);
                        }else{
                            temp = temp - 1800;
                            temp = temp / (3250-1800);
                        }
                        accu = accu*1.0f + temp*0.1f;
                        accu = accu *0.91f;
                    }
                    temp = accu;

                    temp = temp * 100;
                    if (temp < 0) {
                        temp = 0;
                    }
                    // 3300 12.6v=((A1-100)/8.84)+100
                    // 2700 12v
                    // 1850 11.1v
                    // 1000 10v
                    // new 
                    // 9.60v 720  1039
                    // 11.1 1800  2358
                    // 12.6 3250  3753

                    int32_t a = temp;
                    int32_t b = buff.telemData.alt/10;//in 0.01m

                    printInt(b, 4, 0);
                    charbuff[4] = 17;
                    printInt(a, 3, 5);

                    //显示到 Max7219
                    Write_Char_Max7219(1,charbuff[7]);
                    Write_Char_Max7219(2,charbuff[6]);
                    Write_Char_Max7219(3,charbuff[5]);
                    Write_Char_Max7219(4,charbuff[4]);
                    Write_Char_Max7219(5,charbuff[3]);
                    Write_Char_Max7219(6,charbuff[2]);
                    Write_Char_Max7219(7,charbuff[1]);
                    Write_Char_Max7219(8,charbuff[0]);

                }

                //debuginfoFastInt[0] = totalCount;
                //debuginfoFastInt[1] = errorCount;

                if(flg){
                    IOWrite(IO_DEBUG, 1);
                    flg = 0;
                }else{
                    IOWrite(IO_DEBUG, 0);
                    flg = 1;
                }
            }
            //protocolDataReceived = true;

            NRF24L01_ClearAllInterrupts();
        }
    }
    return false;//遥控器永远返回false
#else
    UNUSED(printInt);
    UNUSED(oldFrameCount);
    UNUSED(accu);
    static int flg;
    bool irq = IORead(irqIO);
    if (!irq)//下降
    {
        if(status==1){//发送完成
            uint8_t s = NRF24L01_ReadReg(NRF24L01_07_STATUS);
            if(s & BV(NRF24L01_07_STATUS_TX_DS)){// 已发送
                NRF24L01_ClearAllInterrupts();
                //进入接收模式
                NRF24L01_SetRxMode();
            }
            status=0;
        }else{
            //接收遥控数据
            uint8_t s = NRF24L01_ReadReg(NRF24L01_07_STATUS);
            if( s & BV(NRF24L01_07_STATUS_RX_DR)){
                NRF24L01_ReadPayload(buff.arr, PAYLOAD_SIZE);

                uint32_t crc = 0;
                crc = crc32(crc, buff.arr, DATA_SIZE);
                totalCount++;
                if(buff.crcData.crc != crc){
                    errorCount++;
                    #ifdef LED3_PIN
                        LED3_OFF;
                    #endif
                    #ifdef LED4_PIN
                        LED4_OFF;
                    #endif
                }else{
                    if(ARMING_FLAG(ARMED)){
#ifdef LED3_PIN
                        LED3_ON;
#endif
#ifdef LED4_PIN
                        LED4_ON;
#endif
                    }else{
#ifdef LED3_PIN
                        LED3_OFF;
#endif
#ifdef LED4_PIN
                        LED4_OFF;
#endif
                    }
                    //RETA1234
                    int temp;
                    rawData[0] = 1000 + (((int)buff.controlData.yaw * 1000)>>8);//rudder

                    temp = (buff.controlData.att_pitch>>2);//elevator +=2000
                    temp+=1500;
                    if(temp<1000)temp = 1000;
                    if(temp>2000)temp = 2000;
                    rawData[1] = temp;

                    rawData[2] = 1000 + (((int)buff.controlData.throttle * 1000)>>8);//throttle
                    rx_count = 0;
                    temp = (buff.controlData.att_roll>>2);//a---ron
                    temp+=1500;
                    if(temp<1000)temp = 1000;
                    if(temp>2000)temp = 2000;
                    rawData[3] = temp;

                    target_att[FD_ROLL] = buff.controlData.att_roll;
                    target_att[FD_ROLL] /= 100;

                    target_att[FD_PITCH] = buff.controlData.att_pitch;
                    target_att[FD_PITCH] /= 100;

                    nrf24l01_x = buff.controlData.x;
                    nrf24l01_y = buff.controlData.y;

                    //debuginfoFastInt[15] = buff.controlData.manual;
                    manual = buff.controlData.manual;
                }

                //debuginfoFastInt[0] = totalCount;
                //debuginfoFastInt[1] = errorCount;

                if(flg){
                    IOWrite(IO_DEBUG, 1);
                    flg = 0;
                }else{
                    IOWrite(IO_DEBUG, 0);
                    flg = 1;
                }
            }
            //protocolDataReceived = true;
            NRF24L01_ClearAllInterrupts();

            receiveCount++;

            if(receiveCount>=2){//接收到后发送 接收10次，回送一次
                receiveCount = 0;
                buff.telemData.volt = adcGetChannel(ADC_BATTERY);
                buff.telemData.alt = getEstimatedAltitude();
//仅有的四轴，未定义VOLT_VERSION， VOLT_VERSION == OLD_FRAME
#ifdef VOLT_VERSION
                buff.telemData.volt |= 0x8000;//version bit
#endif
                uint32_t crc = 0;
                crc = crc32(crc, buff.arr, DATA_SIZE);
                buff.crcData.crc = crc;

                NRF24L01_FlushTx();

                NRF24L01_WritePayload(buff.arr, PAYLOAD_SIZE);

                NRF24L01_SetTxMode();
                status=1;//发送后进入发送状态
            }
            return true;
        }
    }
    return false;
#endif
}

uint16_t nrf24l01ReadRaw(uint8_t channel){//1000~2000 TODO NRF24L01
#ifdef IS_FIXED_WIN
    if(channel==2){// 无信号关电机
        if(rx_count<100){
            rx_count++;
            return rawData[channel];
        }else{
            return 1000;
        }
    }else{
        return rawData[channel];
    }
#else
    return rawData[channel];
#endif
}

int16_t att_roll;
int16_t att_pitch;

void beforeProcessRx(){//1000~2000 TODO NRF24L01
//定时发送
#ifdef I_AM_REMOTE_CONTROLLER

    uint16_t adcSample0 = adcGetChannel(ADC_CHANNEL0);
    uint16_t adcSample1 = adcGetChannel(ADC_CHANNEL1);
    uint16_t adcSample2 = adcGetChannel(ADC_CHANNEL2);
    uint16_t adcSample3 = adcGetChannel(ADC_CHANNEL3);
    debuginfoFastInt[5] = attitude.values.att_roll * 100; // 右+ 左-
    debuginfoFastInt[6] = attitude.values.att_pitch * 100; // 前+ 后-
    debuginfoFastInt[7] = adcSample0;//0 左侧左右， 左3618 中值2131 右574
    debuginfoFastInt[8] = adcSample1;//1 左侧上下， 下476 2080 上3570
    debuginfoFastInt[9] = adcSample2;//2 右侧上下   下330  上3584
    debuginfoFastInt[10] = adcSample3;//3 右侧左右   左3550 右483 中值1991
    //

    static int time = 0;
    uint16_t bat = adcGetChannel(ADC_BAT);
    if(bat<2200){
        if(millis()-time > 200){
            time = millis();
            LED2_TOGGLE;
        }
    }

    bool btn = IORead(btnIO);
    bool btn2 = IORead(btn2IO);

    if(btn){
        att_roll = (int16_t)(attitude.values.att_roll * 100);// 右+ 左-
        att_pitch = (int16_t)(attitude.values.att_pitch * 100);
    }else{
        float temp;
        //attitude.values.att_roll * 100; // 右+ 左-
        temp = adcSample0;//x 左侧左右， 左3618 中值2131 右574
        temp = temp - 2131;
        if(temp>0){
            temp = temp / (3618-2131);
        }else{
            temp = temp / (2131-574);
        }
        temp = -temp;
        temp = temp * 2000;//+-500*4
        att_roll = (int16_t)temp;

        //attitude.values.att_pitch * 100; // 前+ 后-
        temp = adcSample1;//y 左侧上下， 下476 2080 上3570
        temp = temp - 2080;
        if(temp>0){
            temp = temp / (3570-2080);
        }else{
            temp = temp / (2080-476);
        }
        temp = temp * 2000;//+-500*4
        att_pitch = (int16_t)temp;
    }

    buff.controlData.att_roll = att_roll;
    buff.controlData.att_pitch = att_pitch;

    float throttle = adcSample2;
    throttle = throttle-336;
    throttle = throttle / (3584 - 330);
    throttle = throttle * 256;
    if(throttle<0)throttle=0;
    if(throttle>255)throttle=255;
    buff.controlData.throttle = (uint8_t)throttle;

    float yaw = adcSample3;
    yaw = yaw - 1990;
    if(yaw>0){
        yaw = yaw / (3550-1990);
    }else{
        yaw = yaw / (1990-483);
    }
    yaw = -yaw;
    yaw = yaw * 128;
    yaw = yaw + 128;
    if(yaw<0)yaw=0;
    if(yaw>255)yaw=255;
    buff.controlData.yaw = (uint8_t)yaw;

    buff.controlData.x = 255 - (adcSample1 >> 4);
    buff.controlData.y = (adcSample0 >> 4);

    buff.controlData.manual = btn2 == 1;//button down = auto rudder
    debuginfoFastInt[15] = buff.controlData.manual;

    uint32_t crc = 0;
    crc = crc32(crc, buff.arr, DATA_SIZE);
    buff.crcData.crc = crc;

    NRF24L01_FlushTx();

    NRF24L01_WritePayload(buff.arr, PAYLOAD_SIZE);

    NRF24L01_SetTxMode();
    status=1;//发送后进入发送状态

    
    if(buff.controlData.x<20){
        beepFlg = false;
    }
    if(buff.controlData.x>235){
        beepFlg = true;
    }

#else
    UNUSED(btnIO);
    UNUSED(btn2IO);
#endif
}

static void NRF24L01_InitGpio(void)
{
    // CE as OUTPUT
    //const SPIDevice rxSPIDevice = spiDeviceByInstance(RX_SPI_INSTANCE);
    //IOInit(DEFIO_IO(RX_CE_PIN), OWNER_RX_SPI_CS, rxSPIDevice + 1);
    IOConfigGPIO(DEFIO_IO(RX_CE_PIN), SPI_IO_CS_CFG);
    
    // NSS as output
    IOConfigGPIO(IOGetByTag(IO_TAG(RX_NSS_PIN)), IO_CONFIG(GPIO_Mode_Out_PP, GPIO_Speed_50MHz));



    irqIO = IOGetByTag(IO_TAG(RX_IRQ_PIN));
    IOConfigGPIO(irqIO, IOCFG_IN_FLOATING);

#ifdef BUTTON_PIN
    btnIO = IOGetByTag(IO_TAG(BUTTON_PIN));
    IOConfigGPIO(btnIO, IOCFG_IN_FLOATING);
#endif
#ifdef BUTTON2_PIN
    btn2IO = IOGetByTag(IO_TAG(BUTTON2_PIN));
    IOConfigGPIO(btn2IO, IOCFG_IN_FLOATING);
#endif


    // irq_extiCallbackRec.fn = irq_extiHandler;
    // EXTIConfig(irqIO, &irq_extiCallbackRec, NVIC_BUILD_PRIORITY(2, 0), EXTI_Trigger_Rising_Falling); // TODO - priority!
    // EXTIEnable(irqIO, true);

    ///////////////////////////////////////////NRF24_CE_LO();
    
}

uint8_t NRF24L01_WriteReg(uint8_t reg, uint8_t data)
{
    return rxSpiWriteCommand(W_REGISTER | (REGISTER_MASK & reg), data);
}

uint8_t NRF24L01_WriteRegisterMulti(uint8_t reg, const uint8_t *data, uint8_t length)
{
    return rxSpiWriteCommandMulti(W_REGISTER | ( REGISTER_MASK & reg), data, length);
}

/*
 * Transfer the payload to the nRF24L01 TX FIFO
 * Packets in the TX FIFO are transmitted when the
 * nRF24L01 next enters TX mode
 */
uint8_t NRF24L01_WritePayload(const uint8_t *data, uint8_t length)
{
    return rxSpiWriteCommandMulti(W_TX_PAYLOAD, data, length);
}

uint8_t NRF24L01_WriteAckPayload(const uint8_t *data, uint8_t length, uint8_t pipe)
{
    return rxSpiWriteCommandMulti(W_ACK_PAYLOAD | (pipe & 0x07), data, length);
}

uint8_t NRF24L01_ReadReg(uint8_t reg)
{
    return rxSpiReadCommand(R_REGISTER | (REGISTER_MASK & reg), NOP);
}

uint8_t NRF24L01_ReadRegisterMulti(uint8_t reg, uint8_t *data, uint8_t length)
{
    return rxSpiReadCommandMulti(R_REGISTER | (REGISTER_MASK & reg), NOP, data, length);
}

/*
 * Read a packet from the nRF24L01 RX FIFO.
 */
uint8_t NRF24L01_ReadPayload(uint8_t *data, uint8_t length)
{
    return rxSpiReadCommandMulti(R_RX_PAYLOAD, NOP, data, length);
}

/*
 * Empty the transmit FIFO buffer.
 */
void NRF24L01_FlushTx(void)
{
    rxSpiWriteByte(FLUSH_TX);
}

/*
 * Empty the receive FIFO buffer.
 */
void NRF24L01_FlushRx(void)
{
    rxSpiWriteByte(FLUSH_RX);
}

uint8_t NRF24L01_Activate(uint8_t code)
{
    return rxSpiWriteCommand(ACTIVATE, code);
}

// standby configuration, used to simplify switching between RX, TX, and Standby modes
static uint8_t standbyConfig;

void NRF24L01_Initialize(uint8_t baseConfig)
{
    standbyConfig = BV(NRF24L01_00_CONFIG_PWR_UP) | baseConfig;
    NRF24L01_InitGpio();
    // nRF24L01+ needs 100 milliseconds settling time from PowerOnReset to PowerDown mode
    static const uint32_t settlingTimeUs = 100000;
    const uint32_t currentTimeUs = micros();
    if (currentTimeUs < settlingTimeUs) {
        delayMicroseconds(settlingTimeUs - currentTimeUs);
    }
    // now in PowerDown mode
    NRF24L01_WriteReg(NRF24L01_00_CONFIG, standbyConfig); // set PWR_UP to enter Standby mode
    // nRF24L01+ needs 4500 microseconds from PowerDown mode to Standby mode, for crystal oscillator startup
    delayMicroseconds(4500);
    // now in Standby mode



    NRF24L01_SetupBasic();

    NRF24L01_SetChannel(CHANNEL);

    NRF24L01_WriteReg(NRF24L01_06_RF_SETUP, DATA_RATE | RF_PWR);
    // RX_ADDR for pipes P2 to P5 are left at default values
    NRF24L01_FlushRx();
#ifdef I_AM_REMOTE_CONTROLLER
    NRF24L01_FlushTx();
#endif
    NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR, txAddr, RX_TX_ADDR_LEN);
    NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0, rxAddr, RX_TX_ADDR_LEN);

    NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, PAYLOAD_SIZE);
#ifdef I_AM_REMOTE_CONTROLLER
#else
    NRF24L01_SetRxMode(); // enter receive mode to start listening for packets
#endif

    //NRF24L01_ClearAllInterrupts();
}

/*
 * Common setup of registers
 */
void NRF24L01_SetupBasic(void)
{
    NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // No auto acknowledgment
    NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, BV(NRF24L01_02_EN_RXADDR_ERX_P0));
    NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, ADDR_WIDTH); // 5-byte RX/TX address
    NRF24L01_WriteReg(NRF24L01_1C_DYNPD, 0x00); // Disable dynamic payload length on all pipes
}

/*
 * Enter standby mode
 */
void NRF24L01_SetStandbyMode(void)
{
    // set CE low and clear the PRIM_RX bit to enter standby mode
    NRF24_CE_LO();
    NRF24L01_WriteReg(NRF24L01_00_CONFIG, standbyConfig);
}

/*
 * Enter receive mode
 */
void NRF24L01_SetRxMode(void)
{
    NRF24_CE_LO(); // drop into standby mode
    // set the PRIM_RX bit
    NRF24L01_WriteReg(NRF24L01_00_CONFIG, standbyConfig | BV(NRF24L01_00_CONFIG_PRIM_RX));
    NRF24L01_ClearAllInterrupts();
    // finally set CE high to start enter RX mode
    NRF24_CE_HI();
    // nRF24L01+ will now transition from Standby mode to RX mode after 130 microseconds settling time
}

/*
 * Enter transmit mode. Anything in the transmit FIFO will be transmitted.
 */
void NRF24L01_SetTxMode(void)
{
    // Ensure in standby mode, since can only enter TX mode from standby mode
    NRF24L01_SetStandbyMode();
    NRF24L01_ClearAllInterrupts();
    // pulse CE for 10 microseconds to enter TX mode
    NRF24_CE_HI();
    delayMicroseconds(10);
    NRF24_CE_LO();
    // nRF24L01+ will now transition from Standby mode to TX mode after 130 microseconds settling time.
    // Transmission will then begin and continue until TX FIFO is empty.
}

void NRF24L01_ClearAllInterrupts(void)
{
    // Writing to the STATUS register clears the specified interrupt bits
    NRF24L01_WriteReg(NRF24L01_07_STATUS, BV(NRF24L01_07_STATUS_RX_DR) | BV(NRF24L01_07_STATUS_TX_DS) | BV(NRF24L01_07_STATUS_MAX_RT));
}

void NRF24L01_SetChannel(uint8_t channel)
{
    NRF24L01_WriteReg(NRF24L01_05_RF_CH, channel);
}

#endif // USE_RX_NRF24
